package com.briup.smartcity.service.impl;

import com.briup.smartcity.bean.Carousel;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.CarouselMapper;
import com.briup.smartcity.mapper.extend.CarouselExtendMapper;
import com.briup.smartcity.service.ICarouselService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class CarouseIServiceImpl implements ICarouselService {

    @Autowired
    private CarouselMapper mapper;
    @Override
    public PageInfo<Carousel> findAll(int pageNum, int pageSize) throws ServiceException {

        PageHelper.startPage(pageNum,pageSize,true);
        List<Carousel> list =  mapper.findAllcarousel();
        //PageInfo<Carousel> info = new PageInfo<>(list);
        return new PageInfo<>(list);
    }

    @Override
    public void deleteById(int id) throws ServiceException {
        Carousel carousel = mapper.selectByPrimaryKey(id);
        System.out.println( "carousel的值："+carousel);
        if(carousel==null){
            throw new ServiceException("此图已不存在此系统中");
        }
        if(carousel.getCarouselStatus()==1){
            throw new ServiceException("此轮播图已启用，不能删除");
        }
        System.out.println(" sdfghj");

        List<Carousel> list = mapper.findAllcarousel();
        if(list.size()==1){
            throw new ServiceException("这是最后一张轮播图，不能删除");
        }
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveCarousel(Carousel carousel) throws ServiceException {
        if(carousel==null){
            throw new ServiceException("值不能为空！！");
        }
        carousel.setCarouselStatus(0);
        mapper.insertSelective(carousel);

    }

    @Override
    public void updateCarousel(Carousel carousel) throws ServiceException {
        //0禁用，1启用
        List<Carousel> list = extendMapper.findYes();
        if(list.size()==1&&carousel.getCarouselStatus()==0){
            throw new ServiceException("至少要开启一个轮播图");
        }
        if(list.size()==5&&carousel.getCarouselStatus()==1){
            throw new ServiceException("每次最多开启5张轮播图");
        }
//        if(carousel.getCarouselStatus()==1){
//            carousel.setCarouselStatus(0);
//        }else{
//            carousel.setCarouselStatus(1);
//        }
        System.out.println(carousel);
        mapper.updateByPrimaryKeySelective(carousel);
    }
    @Autowired
    private CarouselExtendMapper extendMapper;
    @Override
    public List<Carousel> findYesCarousel() throws ServiceException {
        List<Carousel> list = extendMapper.findYes();
        return list;
    }
}
